
// Clase Calendar.

var MonthCalendar_Instances = [];

function MonthCalendar(id)
{
    // Propiedades
    this.id = id;

    this.monthNames = [];
    
    this.cssClass = "";
    this.year = 0;
    this.month = 0;    
    
    this.focuseable = true;
    this.parentID = "";
    this.validDate = false;
    this.isWritten = false;
        
    // Public Functions
    this.show = MonthCalendar_Show;
    this.clone = MonthCalendar_Clone;    
    this.draw = MonthCalendar_Draw;
    this.redraw = MonthCalendar_Redraw;
    this.getDate = MonthCalendar_GetDate;   
    this.setDate = MonthCalendar_SetDate;   
    this.emptyDate = MonthCalendar_EmptyDate;    
            
    // Private Functions
    this.drawTitle = MonthCalendar_DrawTitle;
    this.redrawTitle = MonthCalendar_RedrawTitle;    
    this.drawMonths = MonthCalendar_DrawMonths;
    this.getFocusFunction = MonthCalendar_GetFocusFunction;
    
    // Envents 
    this.onMonthChange = "";
    
    MonthCalendar_Instances[id] = this;
}


// -----------------------------------------------------------------------------------------------------------------
// Public Functions
// -----------------------------------------------------------------------------------------------------------------

function MonthCalendar_Clone(newId)
{
    var newCalendar = new MonthCalendar(newId);

	newCalendar.monthNames = this.monthNames.slice();
	newCalendar.cssClass = this.cssClass;
	
	newCalendar.year = this.year;
    newCalendar.month = this.month;
    
    return newCalendar;
}

function MonthCalendar_Show(container)
{
    var divcont = document.createElement("div");
    divcont.id = "divcont" + this.id;

    var outerTable = document.createElement("table");
    outerTable.id = this.id;
    outerTable.className = this.cssClass;
    divcont.appendChild(outerTable);
    
    // Titulo
    var trTitle = outerTable.insertRow(outerTable.rows.length);
    var tdTitle = trTitle.insertCell(trTitle.cells.length);
    
    this.drawTitle(tdTitle);
    
    // Contenido
    var trMainTable = outerTable.insertRow(outerTable.rows.length);
    var tdMainTable = trMainTable.insertCell(trMainTable.cells.length);

    var mainTable = document.createElement("table");
    mainTable.id = "mainTable_" + this.id;
    tdMainTable.appendChild(mainTable);
    
    this.draw(mainTable);
    
    if(container)
        container.appendChild(divcont);
    else
        return "<div id=\"divcont" + this.id + "\">" + divcont.innerHTML + "</div>";
}

function MonthCalendar_Redraw()
{
    var table = document.getElementById("mainTable_" + this.id);
    this.draw(table);
    this.redrawTitle();    
}

function MonthCalendar_Draw(table)
{
    // Limpio la tabla
    var count = table.rows.length;
    for(var i=0; i<count; i++)
        table.deleteRow(0);

    this.drawMonths(table);
    
    this.isWritten = true;
}

function MonthCalendar_GetDate()
{
    return ({year:this.year, month:this.month, day:1});
}

function MonthCalendar_SetDate(date)
{
    this.year = date.year;
    this.month = date.month;
    this.validDate = true;
    
    if(this.isWritten)
        this.redraw();    
}

function MonthCalendar_EmptyDate()
{
    this.month = 0; 
    this.validDate = false;
    
    if(this.isWritten)
        this.redraw();    
}

// -----------------------------------------------------------------------------------------------------------------
// Private Functions
// -----------------------------------------------------------------------------------------------------------------

function MonthCalendar_DrawTitle(container)
{
    var table = document.createElement("table");
    table.setAttribute("calendar", "yes");
    
    var row = table.insertRow(table.rows.length);
    var cellPreviousYear = row.insertCell(row.cells.length);
    var cellTitle = row.insertCell(row.cells.length);
    var cellNextYear = row.insertCell(row.cells.length);

    // Previous Month    
    cellPreviousYear.setAttribute("align", "left");
    cellPreviousYear.innerHTML = "<A calendar=\"yes\" href=\"javascript:" + this.getFocusFunction() + "MonthCalendar_PreviousYear(\'" + this.id + "\')\"> < <\A>";
    
    // Title
    cellTitle.id = "title_" + this.id;
    cellTitle.className = "title";
    Common.setStyleAttribute(cellTitle, "text-align", "center");
    cellTitle.innerHTML = this.year;

    // Next Month
    cellNextYear.setAttribute("align", "right");
    cellNextYear.innerHTML = "<A calendar=\"yes\" href=\"javascript:" + this.getFocusFunction() + "MonthCalendar_NextYear(\'" + this.id + "\')\"> > <\A>";
    
    container.appendChild(table);
}

function MonthCalendar_RedrawTitle()
{
    var td = document.getElementById("title_" + this.id)
    td.innerHTML = this.year;
}

function MonthCalendar_DrawMonths(table)
{
    var month = 1;

    for (var i = 0; i < 4; i++) {
        var row = table.insertRow(table.rows.length);

        for (var j = 0; j < 3; j++) {
            var cell = row.insertCell(row.cells.length);
        
            if (this.month == month)
                cell.className = "selectedMonth";
            else
                cell.className = "month";
                        
            cell.id = "month_" + month + "_" + this.id;        
            cell.innerHTML = "<a calendar=\"yes\" href=\"javascript:" + this.getFocusFunction() + "MonthCalendar_MonthChange('" + this.id + "', " + month + ")\">" + this.monthNames[month-1] + "</a>";        

            month++;
        }
    }
}            

function MonthCalendar_GetFocusFunction()
{
    if (this.Focuseable)
        return "FocusManager.setFocus(\'" + this.id + "\');";
    else
        return "";  
}

        
// ------------------------------------------------------------------------------------------------------
// Funciones externas.
// ------------------------------------------------------------------------------------------------------

function MonthCalendar_Get(id)
{
    return MonthCalendar_Instances[id];
}

function MonthCalendar_PreviousYear(id)  
{
    var monthCalendar = MonthCalendar_Get(id);
    
    monthCalendar.year--;
    monthCalendar.redraw();
}

function MonthCalendar_NextYear(id)
{
    var monthCalendar = MonthCalendar_Get(id);
    
    monthCalendar.year++;
    monthCalendar.redraw();
}

function MonthCalendar_MonthChange(id, month)
{
    var monthCalendar = MonthCalendar_Get(id);
    var monthCell;
    
    if(monthCalendar.month != 0) {
        monthCell = document.getElementById("month_" + monthCalendar.month + "_" + monthCalendar.id);
        monthCell.className = "month"
    }

    monthCell = document.getElementById("month_" + month + "_" + monthCalendar.id);
    monthCell.className = "selectedMonth"
    
    monthCalendar.month = month;
    
    if(monthCalendar.onMonthChange)
        eval(monthCalendar.onMonthChange + "(" + monthCalendar.year + "," + monthCalendar.month + ")");
    
    if(monthCalendar.parentID)
        DatePicker_RefreshValue(monthCalendar.parentID, monthCalendar.year, monthCalendar.month, 1);
    
    this.validDate = true;  
}

function MonthCalendar_Get(id)
{
    return MonthCalendar_Instances[id];
}

// -----------------------------------------------------------------------------------------------------------------
// Funciones de foco
// -----------------------------------------------------------------------------------------------------------------

FocusManager.registerControl("MonthCalendar_DisposeFocus");

function MonthCalendar_DisposeFocus(id)
{
    // Por ahora si un MonthCalendar pierde el foco no hace nada
}
